package cn.wolfcode.rbac.service.impl;

import cn.wolfcode.rbac.domain.Customer;
import cn.wolfcode.rbac.domain.CustomerTraceHistory;
import cn.wolfcode.rbac.mapper.CustomerMapper;
import cn.wolfcode.rbac.mapper.CustomerTraceHistoryMapper;
import cn.wolfcode.rbac.query.CustomerTraceHistoryQuery;
import cn.wolfcode.rbac.query.QueryObject;
import cn.wolfcode.rbac.service.ICustomerService;
import cn.wolfcode.rbac.service.ICustomerTraceHistoryService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class CustomerTraceHistoryServiceImpl implements ICustomerTraceHistoryService {

    @Autowired
    private CustomerTraceHistoryMapper customerTraceHistoryMapper;
    @Autowired
    private CustomerMapper customerMapper;

    @Override
    @Transactional(readOnly = true)
    public PageInfo query(CustomerTraceHistoryQuery paramQueryObject) {
        PageHelper.startPage(paramQueryObject.getCurrentPage(), paramQueryObject.getPageSize());
        List<CustomerTraceHistory> list = this.customerTraceHistoryMapper.selectForList(paramQueryObject);
        for (CustomerTraceHistory c : list) {
            if (c.getTraceResult() == 1) {
                c.setDisplayTraceResult("差");
            } else if (c.getTraceResult() == 2) {
                c.setDisplayTraceResult("中");
            } else if (c.getTraceResult() == 3) {
                c.setDisplayTraceResult("优");
            }

            if (c.getType() == 0) {
                c.setDisplayStatus("潜在客户");
            } else if (c.getType() == 1) {
                c.setDisplayStatus("正式客户");
            } else if (c.getType() == 2) {
                c.setDisplayStatus("客户池");
            } else if (c.getType() == 3) {
                c.setDisplayStatus("失败客户");
            } else if (c.getType() == 4) {
                c.setDisplayStatus("流失客户");
            }
            if (c.getCustomerId() != null) {
                c.setCustomerName(customerMapper.selectCustomerName(c.getCustomerId()));
            }
        }
        return new PageInfo(list);
    }

    @Override
    public void saveOrUpdate(CustomerTraceHistory customertracehistory) {
        if (customertracehistory.getId() == null) {
            this.customerTraceHistoryMapper.insert(customertracehistory);
        } else {
            this.customerTraceHistoryMapper.updateByPrimaryKey(customertracehistory);
        }
    }

    @Override
    public void delete(Long paramLong) {
        this.customerTraceHistoryMapper.deleteByPrimaryKey(paramLong);
    }

    @Override
    @Transactional(readOnly = true)
    public CustomerTraceHistory get(Long paramLong) {
        return this.customerTraceHistoryMapper.selectByPrimaryKey(paramLong);
    }

    @Override
    @Transactional(readOnly = true)
    public List<CustomerTraceHistory> list() {
        List<CustomerTraceHistory> customerTraceHistories = this.customerTraceHistoryMapper.selectAll();
        return customerTraceHistories;
    }
}
